﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.BaseContent;

namespace Job.Web.userControls.Admin
{
    public partial class AdminUserInfo : BaseCtrl
    {
        AdminUserLogic logic = new AdminUserLogic();

        private int userId
        {
            get { return myR["userId"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ( !this.IsPostBack ) 
            {
                BindRights();
                if ( userId > 0 ) LoadData(userId);
            }
        }

        private void BindRights() 
        {
            List<KeyValuePair<String,String>> rights = Constants.GetAdminRights();
            chkRight.DataSource = rights;
            chkRight.DataValueField = "Key";
            chkRight.DataTextField = "Value";
            chkRight.DataBind();            
        }

        private void LoadData(int userId)
        {
            JobUser user = logic.GetDetailById(userId);
            if (null != user)
            {
                hidUserId.Value = Convert.ToString(user.UserId);
                tbLogin.Text = user.LoginName;
                tbLogin.ReadOnly = true;
                tbName.Text = user.Name;
                tbPassword1.Text = user.Password;
                tbPassword2.Text = user.Password;
                tbMail.Text = user.Email;

                string rights = user.Permissions;
                rights = (null == rights) ? "" : rights.Trim();
                if (!rights.EndsWith(",")) rights += ",";

                if (null == chkRight.Items) return;
                int pos ;
                for (int i = 0; i < chkRight.Items.Count; i++)
                {
                    pos = rights.IndexOf(chkRight.Items[i].Value);
                    chkRight.Items[i].Selected = (pos > -1);
                }
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            JobUser user = new JobUser();
            string id = hidUserId.Value;
            id = (null == id) ? "" : id.Trim();
            if ( id.Equals("0")) id = "";
            if (!id.Equals(""))
            {
                user.UserId = Convert.ToInt32(id);
            }
            user.LoginName = tbLogin.Text.Trim();
            user.Name = tbName.Text.Trim();
            user.Password = tbPassword1.Text;
            user.Email = tbMail.Text.Trim();

            string rights = "";
            if (chkRight.Items != null)
            {
                for (int i = 0; i < chkRight.Items.Count; i++)
                {
                    if (chkRight.Items[i].Selected)
                        rights = rights + chkRight.Items[i].Value + ",";
                }
            }
            user.Permissions = rights;

            if (id.Equals(""))
            {
                if (!logic.Insert(user))
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('此用户名已经存在，请重新输入!')</script>");
                }
            }
            else
            {
                logic.Update(user);
            }

            string url = "AdminMain.aspx?controlType="
                + Convert.ToString((int)Constants.SubjectType.AdminManagerList);
            Response.Redirect(url);
        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            string url = "AdminMain.aspx?controlType="
                + Convert.ToString((int)Constants.SubjectType.AdminManagerList);
            Response.Redirect(url);
        }

    }
}